/*
 * table_uniteChain.c
 *
 *  Created on: 2012-5-14
 *      Author: guanxiaoying
 */

#include "../../parser/coredef.h"
/**/
typedef struct _UNITECHAINTBL
{
	AST		astPrev;
	AST     astNext;
	AST     astParam;
	U2      usKind;
} UNITECHAIN, *PUNITECHAIN;


#define TableSize  (sizeof(UNITECHAIN ))
#define TableType  PUNITECHAIN
#define stTable stUniteChainManager.memoryBlocks
#define maxTable stUniteChainManager.maxRecordNum
#define  maxNumPerBlk  (stUniteChainManager.astMaxNum_Block)

static TABLEMANAGER stUniteChainManager = {
//		U4                  usIdx;
		0,
//		PSZ				    con_pszName;    constant
		__FILE__,
//		U2					con_usSize;		constant
		TableSize,
//		U2					block_num;	　　　　　　　Reset clear  際　要
		0,
//		S4					astMaxNum_Block;     １回設定のみ
		0,
//		S4					astTabNum_LastPage; 　　　　Reset clear  際　要
		0,
//		S4					astMaxNum_LastPage;     Reset clear  際　要
		0,
//		U4					ulMaxByte;	　　　　　　　　　　クリアしない
		0,
//		U4					con_minPageSize;　　　　　　　　　　constant
		0
};
static VOID resetListener( U4  procid, U2 index )
{
	if( procid == stUniteChainManager.u4Idx )
	{
		if( index > 0 && index < MAX_VOLUMN_OF_TABLE )
		{
			stTable[ index-1] = NULL;
		}
	}
}
static TableType core_getTable( AST index )
{
	TableType temp = (TableType)Check_Com( &stUniteChainManager,  resetListener, index );
	if( temp != NULL ){ return &temp[index%maxNumPerBlk]; } return NULL;
}
static BOOL ClearTable( VOID )
{
	return Clear_Com( &stUniteChainManager );
}
static BOOL ResetTable( VOID )
{
	return Reset_Com( &stUniteChainManager );
}
static BOOL AllocTable( AST astReqArg )
{
	return AllocTable_Com( &stUniteChainManager, astReqArg, resetListener );
}
static AST getMaxLineNum( VOID ){
	return maxTable;
}
//ACCESS_IDX_FIELD fileindex;
static AST getPrevOne( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->astPrev;
}
/* set method */
static AST  setPrevOne( AST index, AST idx )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, idx );
		return 0;
	}
	return pTbl->astPrev = idx;
}
//ACCESS_IDX_FIELD fileindex;
static AST getNextOne( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->astNext;
}
/* set method */
static AST  setNextOne( AST index, AST idx )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, idx );
		return 0;
	}
	return pTbl->astNext = idx;
}
static AST getParameter_sym( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	if( pTbl->usKind != KIND_SYMDEFINFO ){
		return 0;
	}
	return pTbl->astParam;
}
/* set method */
static AST  setParameter_sym( AST index, AST idx )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, idx );
		return 0;
	}
	if( pTbl->usKind != 0 && pTbl->usKind != KIND_SYMDEFINFO ){
		Throw_MemoryError( &stUniteChainManager, DATA_SETTING_MISS, index, pTbl->usKind  );
	}
	pTbl->usKind = KIND_SYMDEFINFO ;
	return pTbl->astParam = idx;
}
static AST getParameter_stmt( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	if( pTbl->usKind != KIND_STATEMENTINFO ){
		return 0;
	}
	return pTbl->astParam;
}
/* set method */
static AST  setParameter_stmt( AST index, AST idx )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, idx );
		return 0;
	}
	if( pTbl->usKind != 0 && pTbl->usKind != KIND_STATEMENTINFO ){
		Throw_MemoryError( &stUniteChainManager, DATA_SETTING_MISS, index, pTbl->usKind  );
	}
	pTbl->usKind = KIND_STATEMENTINFO ;
	return pTbl->astParam = idx;
}
static AST getParameter_dec( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	if( pTbl->usKind != KIND_DECLARATION ){
		return 0;
	}
	return pTbl->astParam;
}
/* set method */
static AST  setParameter_dec( AST index, AST idx )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, idx );
		return 0;
	}
	if( pTbl->usKind != 0 && pTbl->usKind != KIND_DECLARATION ){
		Throw_MemoryError( &stUniteChainManager, DATA_SETTING_MISS, index, pTbl->usKind  );
	}
	pTbl->usKind = KIND_DECLARATION ;
	return pTbl->astParam = idx;
}
static U2 getKind( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->usKind;
}
/* set method */
static U2  setKind( AST index, U2 value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, idx );
		return 0;
	}
	return pTbl->usKind = value;
}
const UNITECHAINACCESS stUniteChainAccess = {
//		BOOL (*clear)( VOID );
		ClearTable,
//		BOOL (*reset)( VOID );
		ResetTable,
//		BOOL (*alloc)( AST );
		AllocTable,
//		AST  (*getMax)( VOID );
		getMaxLineNum,
		{
				getKind,
				setKind
		},
		{
				getParameter_sym,
				setParameter_sym
		},
		{
				getParameter_stmt,
				setParameter_stmt
		},
		{
				getParameter_dec,
				setParameter_dec
		},
		{
				getNextOne,
				setNextOne
		},
		{
				getPrevOne,
				setPrevOne
		}
};

